package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "声音管理")
@RestController
@RequestMapping("/api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    /**
     * 分页条件查询声音
     * @param page
     * @param size
     * @return
     */
    @GuiguLogin
    @PostMapping("/findUserTrackPage/{page}/{size}")
    public Result findUserTrackPage(@PathVariable("page") Integer page,
                                    @PathVariable("size") Integer size,
                                    @RequestBody TrackInfoQuery trackInfoQuery) {
        trackInfoQuery.setUserId(AuthContextHolder.getUserId());
        return Result.ok(trackInfoService
                .findUserTrackPage(trackInfoQuery, new Page<TrackListVo>(page, size)));
    }


    /**
     * 声音上传API
     * @param file
     * @return
     */
//    @GuiguLogin
    @PostMapping("/uploadTrack")
    public Result uploadTrack(@RequestBody MultipartFile file) {
        return Result.ok(trackInfoService.uploadTrack(file));
    }

    /**
     * 新增声音
     *
     * @param trackInfoVo
     * @return
     */
    @GuiguLogin
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.saveTrackInfo(trackInfoVo);
        return Result.ok();
    }


    /**
     * 根据id删除声音
     *
     * @param trackId
     * @return
     */
    @GuiguLogin
    @DeleteMapping("/removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable("trackId") Long trackId) {
        trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }

    /**
     * 根据id修改声音 - 回显
     *
     * @param trackId
     * @return
     */
    @GuiguLogin
    @GetMapping("/getTrackInfo/{trackId}")
    public Result getTrackInfo(@PathVariable("trackId") Long trackId) {
        return Result.ok(trackInfoService.getOne(
                new LambdaQueryWrapper<TrackInfo>()
                        .eq(TrackInfo::getUserId, AuthContextHolder.getUserId())
                        .eq(TrackInfo::getId, trackId)
        ));
    }


    /**
     * 修改声音
     *
     * @param trackId
     * @param trackInfoVo
     * @return
     */
    @GuiguLogin
    @PutMapping("/updateTrackInfo/{trackId}")
    public Result updateTrackInfo(@PathVariable("trackId") Long trackId,
                                  @RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(trackId, trackInfoVo);
        return Result.ok();
    }


    /**
     * 专辑详情页面声音列表
     *
     * @param albumId
     * @param page
     * @param size
     * @return
     */
    @GuiguLogin
    @GetMapping("/findAlbumTrackPage/{albumId}/{page}/{size}")
    public Result findAlbumTrack(
            @PathVariable("albumId") Long albumId,
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size) {
        return Result.ok(trackInfoService.findAlbumTrackPage(albumId, page, size));
    }


    /**
     * 声音详情页面
     * 查询声音的统计信息
     * @param trackId
     * @return
     */
    @GetMapping("/getTrackStatVo/{trackId}")
    public Result getTrackStatVo(@PathVariable("trackId") Long trackId) {
        return Result.ok(trackInfoService.getTrackStatVo(trackId));
    }

    /**
     * 查询用户可购买的当前专辑的指定声音和后面所有可购买的声音list
     * @param trackId
     * @return
     */
    @GuiguLogin
    @GetMapping("/findUserTrackPaidList/{trackId}")
    public Result findUserTrackPaidLis(@PathVariable("trackId") Long trackId){
        return Result.ok(trackInfoService.findUserTrackPaidLis(trackId));
    }



}

